<template>
  <div>
    <el-dialog title="收货地址" :visible.sync="is.show" :before-close="cancel">
      <el-form :model="seckForm" ref="seckForm" :rules="rules">
        <!-- 活动名称 -->
        <el-form-item
          label="活动名称"
          prop="title"
          :label-width="formLabelWidth"
        >
          <el-input v-model="seckForm.title" autocomplete="off"></el-input>
        </el-form-item>

        <!-- 活动期限 -->
        <el-form-item
          label="活动期限"
          prop="time"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="arr"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
          >
          </el-date-picker>
        </el-form-item>

        <!-- 一级分类 -->
        <el-form-item
          label="一级分类"
          prop="first_cateid"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="seckForm.first_cateid"
            placeholder="请选择活动区域"
            @change="changeFirst"
          >
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- 二级分类 -->
        <el-form-item
          label="二级分类"
          prop="second_cateid"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="seckForm.second_cateid"
            placeholder="请选择活动区域"
            @change="changeSecond"
          >
            <el-option
              v-for="item in secondArr"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- 商品 -->
        <el-form-item label="商品" prop="goodsid" :label-width="formLabelWidth">
          <el-select v-model="seckForm.goodsid" placeholder="请选择活动区域">
            <el-option
              v-for="item in goodsArr"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- 状态 -->
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="seckForm.status"
            active-color="#13ce66"
            inactive-color="#ccc"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="enter">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
//引入辅助性函数
import { mapState, mapActions } from "vuex";
//引入获取具体商品的接口 编辑或添加接口  获取一条数据
import { getgoodslistbysid, addOreditSeck, seckInfo } from "../../request/api";
export default {
  data() {
    //自定义验证规则
    const getTime = (a, b, callback) => {
      if (!this.arr)return
      if (this.arr.length != 2) {
        callback(new Error("请选择活动期限"));
      } else {
        callback();
      }
    };
    return {
      //表单数据
      seckForm: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
      formLabelWidth: "120px",
      secondArr: [], //二级分类的数组
      goodsArr: [], //商品的数组
      arr: [], //活动期限的数组
      //表单验证
      rules: {
        first_cateid: {
          required: true,
          message: "请选择一级分类",
          trigger: "blur",
        },
        second_cateid: {
          required: true,
          message: "请选二级分类",
          trigger: "blur",
        },
        time: { required: true, validator: getTime, trigger: "blur" },
        title: { required: true, message: "请输入活动名称", trigger: "blur" },
        status: { required: true, message: "请选择活动状态", trigger: "blur" },
        goodsid: { required: true, message: "请选择活动商品", trigger: "blur" },
      },
    };
  },

  //计算属性  使用辅助性函数  mapState
  computed: {
    ...mapState("seckill", ["is"]),
    //获取分类数据
    ...mapState("cate", ["cateList"]),
  },

  //方法 使用辅助性函数 mapAction
  methods: {
    ...mapActions("seckill", ["getSECKILLList"]),
    //分类数据的方法
    ...mapActions("cate", ["getCate"]),
    //回显数据
    dataForm(id) {
      // console.log(id);
      seckInfo({ id }).then((res) => {
        this.seckForm = res.list;
        this.seckForm.id = id;
        //转换时间戳
        this.arr = [
          new Date(Number(res.list.begintime)),
          new Date(Number(res.list.endtime)),
        ];
        //获取二级分类数据
        let arr = this.cateList.filter(
          (val) => res.list.first_cateid == val.id
        );
        this.secondArr = arr[0].children;
        //获取商品数据
        getgoodslistbysid({ sid: res.list.second_cateid }).then((res) => {
          this.goodsArr = res.list;
        });
      });
    },

    //点击取消事件
    cancel() {
      //关闭弹窗
      this.$store.commit("seckill/changeDialog", false);
      //清空表单
      this.seckForm = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      };
      //清空时间数组
      this.arr = [];
      //清空表单验证规则
      this.$refs.seckForm.resetFields();
    },

    //点击确认事件
    enter() {
      let url = this.seckForm.id ? "seckedit" : "seckadd";
      //表单验证
      this.$refs.seckForm.validate((val) => {
        if (val) {
          //转换开始时间戳
          this.seckForm.begintime = this.arr[0].getTime();
          //转换结束时间戳
          this.seckForm.endtime = this.arr[1].getTime();
          //调用接口
          addOreditSeck(url, this.seckForm).then((res) => {
            if (res.code == 200) {
              //成功提示
              this.$message.success(res.msg);
              //关闭弹窗
              this.cancel();
              //重新获取数据
              this.getSECKILLList();
            } else {
              //失败提示
              this.$message.error(res.msg);
            }
          });
        }
      });
    },

    //一级分类下拉菜单选中事件
    changeFirst(id) {
      //清空二级分类 和 商品的框
      this.seckForm.second_cateid = "";
      this.seckForm.goodsid = "";
      let arr = this.cateList.filter((val) => id == val.id);
      this.secondArr = arr[0].children;
    },

    //二级分类下拉菜单选中事件
    changeSecond(sid) {
      //清空商品的框
      this.seckForm.goodsid = "";
      //获取具体商品 接口实现
      getgoodslistbysid({ sid }).then((res) => {
        this.goodsArr = res.list;
      });
    },
  },

  //挂载完成
  mounted() {
    if (this.$store.state.cate.cateList.length == 0) {
      this.getCate();
    }
  },
};
</script>


<style scoped>
</style>